package com.zucc.app.controllers;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zucc.app.domains.Appraise;
import com.zucc.app.domains.ClassInfo;
import com.zucc.app.domains.User;
import com.zucc.app.mappers.AppraiseMapper;
import com.zucc.app.mappers.ClassMapper;
import com.zucc.app.mappers.UserMapper;
import freemarker.template.utility.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by hegy on 2018/4/13.
 */
@Controller
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ClassMapper classMapper;

    @Autowired
    private AppraiseMapper appraiseMapper;

    @RequestMapping(value = "/main",method = RequestMethod.GET)
    public String main(){
        return "login";
    }

    @RequestMapping(value = "/main",method = RequestMethod.POST)
    public String remain(Model model,HttpServletRequest req){
        String message = (String)req.getAttribute("message");
        model.addAttribute("message",message);
        return "login";
    }

    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String register(){
        return "register";
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String register(User user){
        try {
            userMapper.insert(user.getUserName(), user.getPassword(), user.getSex());
            return "login";
        } catch (Exception e) {
            e.printStackTrace();
            return "register";
        }
    }

    @RequestMapping(value = "/index",method = RequestMethod.POST)
    public String index(Model model, User user, HttpServletResponse resp, HttpServletRequest req){

        if (StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getPassword())) {
            if (StringUtils.isEmpty(user.getUserName()))
                req.setAttribute("message","请输入用户名");
            else if (StringUtils.isEmpty(user.getPassword()))
                req.setAttribute("message","请输入密码");
            return  "forward:/login/main";
        }
        User reUser = userMapper.select(user.getUserName(), user.getPassword());
        if (reUser == null) {
            req.setAttribute("message","用户名或密码错误");
            return  "forward:/login/main";
        }
        Cookie cookie = new Cookie(reUser.getUserName(), reUser.getId().toString());
        resp.addCookie(cookie);
        return "index";
    }

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "index";
    }

    @RequestMapping(value = "/classList",method = RequestMethod.POST)
    @ResponseBody
    public JSON classList(HttpServletRequest req){
        JSONObject json = new JSONObject();
        List<ClassInfo> list = classMapper.selectList();
        json.put("list",list);
        Cookie[] cookies = req.getCookies();
        json.put("id",cookies[0].getValue());
        return json;
    }

    @RequestMapping(value = "/appraise",method = RequestMethod.GET)
    public String appraise(Model model,Integer id){
        model.addAttribute("id",id);
        return "appraise";
    }

    @RequestMapping(value = "/appraiseList",method = RequestMethod.POST)
    @ResponseBody
    public JSON appraiseList(Integer id){
        JSONObject json = new JSONObject();
        List<Appraise> list = appraiseMapper.select(id);
        List<Appraise> sexList = appraiseMapper.countSex(id);
        List<Map<String, Integer>> gradeList = appraiseMapper.countGrade(1);
        json.put("list",list);
        json.put("sexList",sexList);
        json.put("gradeList",gradeList);
        return json;
    }

    @RequestMapping(value = "/appraiseClass",method = RequestMethod.POST)
    @ResponseBody
    public JSON appraiseClass(Appraise appraise){
        JSONObject json = new JSONObject();
        Appraise info = appraiseMapper.checkInfo(appraise.getClassId(), appraise.getUserId());
        json.put("info", info);
        return json;
    }

    @RequestMapping(value = "/saveAppraise",method = RequestMethod.POST)
    @ResponseBody
    public JSON saveAppraise(Appraise appraise){
        JSONObject json = new JSONObject();
        int result = appraiseMapper.insert(appraise.getClassId(), appraise.getUserId(), appraise.getAppraiseInfo(), appraise.getGrade());
        json.put("result", result == 1 ? "success" : "fail");
        return json;
    }
}
